<!doctype html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=robots content="noodp"><meta http-equiv=x-ua-compatible content="IE=edge, chrome=1"><title>Git常用命令 - Yulin Lewis' Blog</title><meta name=keywords content="雨临Lewis,Java,hugo,hexo,博客"><meta name=Description content="不想当写手的码农不是好咸鱼_(xз」∠)_"><meta property="og:title" content="Git常用命令"><meta property="og:description" content="git clone
克隆远程库到本地：


1
2
3
4
5
6
7


// 默认本地仓库名字与远程库一样
git clone https://github.com/jquery/jquery.git

// 指定本地库名字为test
git clone https://github.com/jquery/jquery.git test

// git clone的仓库地址支持多种协议，通常使用最多的是http(s)和ssh协议，比如从GitHub上克隆仓库。"><meta property="og:type" content="article"><meta property="og:url" content="https://lewky.cn/posts/git-commands/"><meta property="og:image" content="https://lewky.cn/logo.png"><meta property="article:published_time" content="2021-11-02T23:31:23+08:00"><meta property="article:modified_time" content="2022-01-04T23:31:23+08:00"><meta name=twitter:card content="summary_large_image"><meta name=twitter:image content="https://lewky.cn/logo.png"><meta name=twitter:title content="Git常用命令"><meta name=twitter:description content="git clone
克隆远程库到本地：


1
2
3
4
5
6
7


// 默认本地仓库名字与远程库一样
git clone https://github.com/jquery/jquery.git

// 指定本地库名字为test
git clone https://github.com/jquery/jquery.git test

// git clone的仓库地址支持多种协议，通常使用最多的是http(s)和ssh协议，比如从GitHub上克隆仓库。"><meta name=application-name content="雨临Lewis的博客"><meta name=apple-mobile-web-app-title content="雨临Lewis的博客"><meta name=theme-color content="#ffffff"><meta name=msapplication-TileColor content="#da532c"><link rel="shortcut icon" type=image/x-icon href=/favicon.ico><link rel=icon type=image/png sizes=32x32 href=/favicon-32x32.png><link rel=icon type=image/png sizes=16x16 href=/favicon-16x16.png><link rel=apple-touch-icon sizes=180x180 href=/apple-touch-icon.png><link rel=mask-icon href=/safari-pinned-tab.svg color=#5bbad5><link rel=manifest href=/site.webmanifest><link rel=canonical href=https://lewky.cn/posts/git-commands/><link rel=prev href=https://lewky.cn/posts/network/><link rel=next href=https://lewky.cn/posts/postgresql-dml/><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css><link rel=stylesheet href=/css/style.min.css><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css><script type=application/ld+json>{"@context":"http://schema.org","@type":"BlogPosting","headline":"Git常用命令","inLanguage":"","mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/lewky.cn\/posts\/git-commands\/"},"genre":"posts","keywords":"Git, 版本控制工具","wordcount":3931,"url":"https:\/\/lewky.cn\/posts\/git-commands\/","datePublished":"2021-11-02T23:31:23+08:00","dateModified":"2022-01-04T23:31:23+08:00","license":"This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher":{"@type":"Organization","name":"雨临Lewis","logo":"https:\/\/lewky.cn\/images\/avatar.jpg"},"author":{"@type":"Person","name":"雨临Lewis"},"description":""}</script></head><body header-desktop=fixed header-mobile=auto><script type=text/javascript>(window.localStorage&&localStorage.getItem('theme')?localStorage.getItem('theme')==='dark':('auto'==='auto'?window.matchMedia('(prefers-color-scheme: dark)').matches:'auto'==='dark'))&&document.body.setAttribute('theme','dark');</script><div id=mask></div><div class=wrapper><header class=desktop id=header-desktop><div class=header-wrapper><a href=https://github.com/lewky class=github-corner target=_blank title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="3.5rem" height="3.5rem" viewBox="0 0 250 250" style="fill:#70b7fd;color:#fff;position:absolute;top:0;border:0;left:0;transform:scale(-1,1)" aria-hidden="true"><path d="M0 0 115 115h15l12 27L250 250V0z"/><path d="M128.3 109C113.8 99.7 119 89.6 119 89.6 122 82.7 120.5 78.6 120.5 78.6 119.2 72 123.4 76.3 123.4 76.3 127.3 80.9 125.5 87.3 125.5 87.3 122.9 97.6 130.6 101.9 134.4 103.2" fill="currentcolor" style="transform-origin:130px 106px" class="octo-arm"/><path d="M115 115C114.9 115.1 118.7 116.5 119.8 115.4L133.7 101.6C136.9 99.2 139.9 98.4 142.2 98.6 133.8 88 127.5 74.4 143.8 58 148.5 53.4 154 51.2 159.7 51 160.3 49.4 163.2 43.6 171.4 40.1 171.4 40.1 176.1 42.5 178.8 56.2 183.1 58.6 187.2 61.8 190.9 65.4 194.5 69 197.7 73.2 200.1 77.6 213.8 80.2 216.3 84.9 216.3 84.9 212.7 93.1 206.9 96 205.4 96.6 205.1 102.4 203 107.8 198.3 112.5 181.9 128.9 168.3 122.5 157.7 114.1 157.9 116.9 156.7 120.9 152.7 124.9L141 136.5C139.8 137.7 141.6 141.9 141.8 141.8z" fill="currentcolor" class="octo-body"/></svg></a><div class=header-title><a href=/ title="Yulin Lewis' Blog"><span class=header-title-pre><i class="fas fa-fw fa-atom"></i></span>雨临Lewis</a></div><div class=menu><div class=menu-inner><div class=dropdown><a href=/posts/ class="menu-item menu-more dropbtn" title=点击查看所有文章><i class="fas fa-fw fa-archive"></i>归档</a><div class="menu-more-content dropdown-content"><a href=/categories/><i class="fas fa-fw fa-th"></i>分类 </a><a href=/tags/><i class="fas fa-fw fa-tag"></i>标签 </a><a href=/hot/ title=文章热度Top15><i class="fas fa-fw fa-fire"></i>热度 </a><a href=/donation/ title=感谢打赏，老板大气~><i class="fas fa-fw fa-donate"></i>打赏 </a><a href=/about/><i class="fas fa-fw fa-at"></i>关于</a></div></div><a class=menu-item href=/friends/ title=欢迎申请友链><i class="fas fa-fw fa-link"></i>友链</a><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn" title=一些有意思的东东~><i class="fas fa-fw fa-fan fa-spin"></i>趣味</a><div class="menu-more-content dropdown-content"><a href=/funny/high/ title="前方高能♂ 小心外放！" target=_blank rel=noopener><i class="fas fa-fw fa-dizzy"></i>燥起来！ </a><a href=/funny/mikutap/ title=初音未来音乐游戏 target=_blank rel=noopener><i class="fas fa-fw fa-music"></i>在线打碟 </a><a href=/funny/catch-the-cat/ title=逮住那只猫!><i class="fas fa-fw fa-cat"></i>抓住猫咪</a></div></div><div class=dropdown><a href=/ class="menu-item menu-more dropbtn" title=回到首页><i class="fas fa-fw fa-atom"></i>站点</a><div class="menu-more-content dropdown-content"><a href=/bbs/ title=来留言吧~><i class="fas fa-fw fa-comment"></i>公告留言 </a><a href=https://seo.chinaz.com target=_blank rel=noopener><i class="fas fa-fw fa-chart-line"></i>SEO查询 </a><a href=https://www.ping.cn/http/lewky.cn target=_blank rel=noopener><i class="fas fa-fw fa-bezier-curve"></i>网站测速 </a><a href=/posts/e62c38c4.html/><i class="fas fa-fw fa-cog fa-spin"></i>建站日志</a></div></div><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn"><i class="fas fa-fw fa-book"></i>文档</a><div class="menu-more-content dropdown-content"><a href=https://gohugo.io/documentation/ target=_blank rel=noopener><i class="fas fa-fw fa-star"></i>Hugo文档 </a><a href=https://javanote.doc.lewky.cn/ title=尚在完善中~ target=_blank rel=noopener><i class="fab fa-fw fa-java"></i>Java 笔记</a></div></div><a class=menu-item href=/search/ title=渲染搜索结果需要等待几秒钟~><i class="fas fa-fw fa-search"></i>搜索
</a><span class="menu-item delimiter"></span><a href=https://travellings.link target=_blank class=menu-item rel=noopener title=开往-友链接力><i class="fas fa-fw fa-subway"></i></a><a href=https://rssblog.vercel.app/ class=menu-item target=_blank rel=noopener title=RSSBlog><i class="fas fa-fw fa-inbox"></i></a><a href=https://github.com/lewky class=menu-item target=_blank rel=noopener title=GitHub><i class="fab fa-fw fa-github"></i></a></div></div></div></header><header class=mobile id=header-mobile><div class=header-container><div class=header-wrapper><div class=header-title><a href=/ title="Yulin Lewis' Blog"><span class=header-title-pre><i class="fas fa-fw fa-atom"></i></span>雨临Lewis</a></div><div class=menu-toggle id=menu-toggle-mobile><span></span><span></span><span></span></div></div><div class=menu id=menu-mobile><div class=dropdown><a href=/posts/ class="menu-item menu-more dropbtn" title=点击查看所有文章><i class="fas fa-fw fa-archive"></i>归档</a><div class="menu-more-content dropdown-content"><a href=/categories/><i class="fas fa-fw fa-th"></i>分类 </a><a href=/tags/><i class="fas fa-fw fa-tag"></i>标签 </a><a href=/hot/ title=文章热度Top15><i class="fas fa-fw fa-fire"></i>热度 </a><a href=/donation/ title=感谢打赏，老板大气~><i class="fas fa-fw fa-donate"></i>打赏 </a><a href=/about/><i class="fas fa-fw fa-at"></i>关于</a></div></div><a class=menu-item href=/friends/ title=欢迎申请友链><i class="fas fa-fw fa-link"></i>友链</a><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn" title=一些有意思的东东~><i class="fas fa-fw fa-fan fa-spin"></i>趣味</a><div class="menu-more-content dropdown-content"><a href=/funny/high/ title="前方高能♂ 小心外放！" target=_blank rel=noopener><i class="fas fa-fw fa-dizzy"></i>燥起来！ </a><a href=/funny/mikutap/ title=初音未来音乐游戏 target=_blank rel=noopener><i class="fas fa-fw fa-music"></i>在线打碟 </a><a href=/funny/catch-the-cat/ title=逮住那只猫!><i class="fas fa-fw fa-cat"></i>抓住猫咪</a></div></div><div class=dropdown><a href=/ class="menu-item menu-more dropbtn" title=回到首页><i class="fas fa-fw fa-atom"></i>站点</a><div class="menu-more-content dropdown-content"><a href=/bbs/ title=来留言吧~><i class="fas fa-fw fa-comment"></i>公告留言 </a><a href=https://seo.chinaz.com target=_blank rel=noopener><i class="fas fa-fw fa-chart-line"></i>SEO查询 </a><a href=https://www.ping.cn/http/lewky.cn target=_blank rel=noopener><i class="fas fa-fw fa-bezier-curve"></i>网站测速 </a><a href=/posts/e62c38c4.html/><i class="fas fa-fw fa-cog fa-spin"></i>建站日志</a></div></div><div class=dropdown><a href=javascript:void(0); class="menu-item menu-more dropbtn"><i class="fas fa-fw fa-book"></i>文档</a><div class="menu-more-content dropdown-content"><a href=https://gohugo.io/documentation/ target=_blank rel=noopener><i class="fas fa-fw fa-star"></i>Hugo文档 </a><a href=https://javanote.doc.lewky.cn/ title=尚在完善中~ target=_blank rel=noopener><i class="fab fa-fw fa-java"></i>Java 笔记</a></div></div><a class=menu-item href=/search/ title=渲染搜索结果需要等待几秒钟~><i class="fas fa-fw fa-search"></i>搜索</a>
<a href=https://travellings.link target=_blank class=menu-item rel=noopener title=开往-友链接力><i class="fas fa-fw fa-subway"></i></a><a href=https://rssblog.vercel.app/ class=menu-item target=_blank rel=noopener title=RSSBlog><i class="fas fa-fw fa-inbox"></i></a><a href=https://github.com/lewky class=menu-item target=_blank rel=noopener title=GitHub><i class="fab fa-fw fa-github"></i></a></div></div></header><div class="search-dropdown desktop"><div id=search-dropdown-desktop></div></div><div class="search-dropdown mobile"><div id=search-dropdown-mobile></div></div><main class=main><div class=container><div class=toc id=toc-auto><h2 class=toc-title>目录</h2><div class=toc-content id=toc-content-auto></div></div><article class="page single"><h1 class="single-title animated flipInX">Git常用命令</h1><div class=post-meta><div class=post-meta-line><span class=post-author><a href=/ title=Author rel=author class=author><i class="fas fa-user-circle fa-fw"></i>雨临Lewis</a></span>&nbsp;<span class=post-category>收录于 <a href=/categories/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E5%B7%A5%E5%85%B7/><i class="far fa-folder fa-fw"></i>版本控制工具</a></span></div><div class=post-meta-line><i class="far fa-calendar fa-fw"></i>&nbsp;<time datetime=2021-11-02>2021-11-02</time>&nbsp;<i class="far fa-calendar-plus fa-fw"></i>&nbsp;<time datetime=2022-01-04>2022-01-04</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 3931 字
<i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 8 分钟&nbsp;<span id=/posts/git-commands/ class=leancloud_visitors data-flag-title=Git常用命令>
<i class="far fa-eye fa-fw"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
</span>&nbsp;
<a href=#comments id=post-meta-vcount title=查看评论><i class="fas fa-comment fa-fw"></i>&nbsp;<span id=/posts/git-commands/ class=waline-comment-count></span>&nbsp;条评论</a></div></div><div class="details toc" id=toc-static kept><div class="details-summary toc-title"><span>目录</span>
<span><i class="details-icon fas fa-angle-right"></i></span></div><div class="details-content toc-content" id=toc-content-static><nav id=TableOfContents><ol><li><a href=#git-clone>git clone</a></li><li><a href=#git-remote>git remote</a></li><li><a href=#git-branch>git branch</a></li><li><a href=#git-checkout>git checkout</a></li><li><a href=#git-fetch>git fetch</a></li><li><a href=#git-pull>git pull</a></li><li><a href=#git-add>git add</a></li><li><a href=#git-push>git push</a></li><li><a href=#git-reset>git reset</a><ol><li><a href=#--hard><code>--hard</code></a></li><li><a href=#--soft><code>--soft</code></a></li><li><a href=#--mixed><code>--mixed</code></a></li><li><a href=#具体操作>具体操作</a></li></ol></li><li><a href=#git-tag>git tag</a></li><li><a href=#参考链接>参考链接</a></li></ol></nav></div></div><div class=content id=content><h2 id=git-clone>git clone</h2><p>克隆远程库到本地：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span><span class=lnt>7
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 默认本地仓库名字与远程库一样
git clone https://github.com/jquery/jquery.git

// 指定本地库名字为test
git clone https://github.com/jquery/jquery.git test

// git clone的仓库地址支持多种协议，通常使用最多的是http(s)和ssh协议，比如从GitHub上克隆仓库。
</code></pre></td></tr></table></div></div><h2 id=git-remote>git remote</h2><p>该命令用于管理远程库，当clone远程库到本地时，远程库将被自动命名为<code>origin</code>。</p><p><code>git remote</code>可以查询当前仓库下的所有远程库：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>$ git remote
origin
</code></pre></td></tr></table></div></div><p><code>git remote -v</code>可以查询当前仓库下的所有远程库对应的地址，一个远程库对应fetch和push两种操作，所以有两个地址。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>$ git remote -v
origin  git@github.com:jquery/jquery.git (fetch)
origin  git@github.com:jquery/jquery.git (push)
</code></pre></td></tr></table></div></div><p><code>git clone -o</code>可以指定远程库的别名：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>$ git clone -o jQuery https://github.com/jquery/jquery.git
$ git remote
jQuery
</code></pre></td></tr></table></div></div><p>其他操作：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 查看远程库的详细信息
git remote show &lt;远程库别名&gt;

// 添加新的远程库
git remote add &lt;远程库别名&gt; &lt;网址&gt;

// 删除远程库
git remote rm &lt;远程库别名&gt;

// 重命名远程库的别名
git remote rename &lt;原远程库别名&gt; &lt;新远程库别名&gt;
</code></pre></td></tr></table></div></div><h2 id=git-branch>git branch</h2><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span><span class=lnt>19
</span><span class=lnt>20
</span><span class=lnt>21
</span><span class=lnt>22
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 创建本地分支
git branch &lt;分支名&gt;

// 删除本地分支
git branch -d &lt;分支名&gt;
git branch --delete &lt;分支名&gt;

// 设置关联远程分支，新建的本地分支需要关联远程分支，被关联的远程分支要存在否则会报错
git branch --set-upstream-to=&lt;远程库别名&gt;/&lt;远程分支名&gt; &lt;本地分支名&gt;

// 查看当前分支
git branch

// 查看远程分支
$ git branch -r
origin/master

// 查看所有分支
// * master表明当前分支是master，对应的远程分支是origin/master
$ git branch -a
* master
  remotes/origin/master
</code></pre></td></tr></table></div></div><h2 id=git-checkout>git checkout</h2><p>用于检出分支，从当前分支切换到另一个分支。切换分支时，要注意已暂存的文件或者提交。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span><span class=lnt>7
</span><span class=lnt>8
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 切换到另一个已存在的分支
git checkout &lt;分支名&gt;

// 基于当前分支创建并切换到新的分支
git checkout -b &lt;新的分支名&gt;

// 基于远程分支创建并切换到新的分支
git checkout -b &lt;新的分支名&gt; &lt;远程库别名&gt;/&lt;远程分支名&gt;
</code></pre></td></tr></table></div></div><h2 id=git-fetch>git fetch</h2><p>该命令用于将远程库的更新拉取到本地库：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 获取远程库所有分支的更新到本地库
git fetch &lt;远程库别名&gt;

// 获取指定分支的更新到本地库
git fetch &lt;远程库别名&gt; &lt;分支名&gt;
</code></pre></td></tr></table></div></div><p>如果远程库的某个分支被删掉了，git fetch默认不会把这个删除操作同步到本地库，依然会看到被删掉的远程分支，这时候可以通过加上<code>-p</code>或<code>--prune</code>参数来将被删掉的远程分支更新到本地：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>git fetch --prune origin 
git fetch -p origin
</code></pre></td></tr></table></div></div><h2 id=git-pull>git pull</h2><p>该命令用于取回远程分支的更新，并将其合并到本地分支。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span><span class=lnt>7
</span><span class=lnt>8
</span><span class=lnt>9
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 取回并合并到指定的本地分支
git pull &lt;远程库别名&gt; &lt;远程分支名&gt;:&lt;本地分支名&gt;

// 如果是合并到当前分支可以不需要指明本地分支名
// git pull相当于git fetch + git merge
git pull &lt;远程库别名&gt; &lt;远程分支名&gt;

// 指定rebase方式合并
git pull --rebase &lt;远程库别名&gt; &lt;远程分支名&gt;:&lt;本地分支名&gt;
</code></pre></td></tr></table></div></div><p>git有两种合并方式：merge和rebase，这两个合并方式存在一定的区别，不能误用。</p><h2 id=git-add>git add</h2><p><code>git add [参数] &lt;路径></code>用于将工作区的文件添加到暂存区，可以多次使用<code>git add</code>命令将文件添加到暂存区中。暂存区中的文件可以通过<code>git commit</code>一次性提交到本地仓库。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 将当前路径下修改的文件以及新增的文件添加到暂存区，不包括被删除的文件
git add .

// 将当前路径下修改的文件和被删除的文件添加到暂存区，不包括新增的文件
// 相当于git add --update
git add -u .

// 将当前路径下的所有文件添加到暂存区，相当于上面两个命令的集合
// 相当于git add --all
git add -A .
</code></pre></td></tr></table></div></div><h2 id=git-push>git push</h2><p>用于推送本地分支的更新到远程分支，用法与git pull类似。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 推送本地分支到远程分支，注意这里的两个分支名顺序和pull是相反的
git push &lt;远程库别名&gt; &lt;本地分支名&gt;:&lt;远程分支名&gt;

// 远程分支名可以省略，会自动推送到有关联关系的远程分支（通常都是和本地分支同名的），如果该远程分支不存在则会新建远程分支
git push &lt;远程库别名&gt; &lt;本地分支名&gt;

// 本地分支名可以省略，此时表示推送空分支到远程分支
// 即删除远程分支
// demo: git push origin :master
git push &lt;远程库别名&gt; :&lt;远程分支名&gt;

// 删除远程分支
// demo: git push origin --delete master
git push &lt;远程库别名&gt; --delete &lt;远程分支名&gt;

// 推送本地所有分支到远程库
// demo: git push --all origin
git push --all &lt;远程库别名&gt;
</code></pre></td></tr></table></div></div><p>在push时，如果远程库的分支版本更新，则会push当前分支失败，需要先git pull合并到当前分支（可能需要解决冲突），然后再重新push。或者可以使用<code>--force</code>参数，强制覆盖掉远程分支，但是一般不建议这样做。</p><p>此外，git push不会推送tag，除非添加<code>--tags</code>参数。</p><h2 id=git-reset>git reset</h2><p>该命令用于将仓库代码回退到指定的版本，通常会带上<code>--hard</code>参数。本地git仓库分为三个区域：工作区，暂存区和本地库。回退版本的命令有3个参数，与这三个区域有关。</p><h3 id=--hard><code>--hard</code></h3><ul><li>移动本地库HEAD指针</li><li>重置工作区</li><li>重置暂存区</li></ul><p>回退版本后，本地库原本工作区和暂存区的文件将被清空，彻底变回指定版本的状态。</p><h3 id=--soft><code>--soft</code></h3><ul><li>移动本地库HEAD指针</li></ul><p>回退版本后，仅仅移动了本地库的指针，工作区和暂存区文件保持不变。</p><h3 id=--mixed><code>--mixed</code></h3><ul><li>移动本地库HEAD指针</li><li>重置暂存区</li></ul><p>回退版本后，本地库原本暂存区的文件将被清空。</p><p>可以通过实际情况来决定使用哪个参数，但在实际开发中，为了避免自己的工作成果被误删，最好先确保自己的改动已经提交到了本地库或者远程库。</p><p>对于误操作回退版本导致文件丢失的情况，可以参考这篇文章：<a href=https://blog.csdn.net/shenlf_bk/article/details/106622877 target=_blank rel="noopener noreffer">关于git reset &ndash;hard命令！！！，未提交代码丢失找回</a></p><h3 id=具体操作>具体操作</h3><p>首先切换到需要回退的分支：<code>git checkout &lt;分支名></code>。</p><p>然后用<code>git reset</code>命令来回退到指定的版本，通常会带上<code>--hard</code>参数：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span><span class=lnt>7
</span><span class=lnt>8
</span><span class=lnt>9
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 回退到上一个版本
git reset --hard HEAD^

// 回退到上上个版本
git reset --hard HEAD^^

// 每多回退一个版本，就多一个^
// 为了避免^太多，可以用~n来简化，n表示回退几次提交，默认是一次
git reset --hard HEAD~2
</code></pre></td></tr></table></div></div><p>此外，也可以用commit id来回退到指定的版本。仓库的每次提交都会为其生成一个唯一的id（40位哈希值），可以通过<code>git log</code>来查看每个提交对应的commit id。</p><p>在回退版本时，可以用这个commit id来指定回退到对应的提交，不需要指定完整的commit id，只需要前7位字符（short commit id）即可。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 比如仓库有一个提交：commit 26e1d228c71e69f0cb63fa73db8cc1ae3c6d8e87
// 现在回退到这一个提交时的版本
git reset --hard 26e1d22
</code></pre></td></tr></table></div></div><p>如果不放心这个手动截取前七位字符的short commit id，可以用以下命令来得到：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>git rev-parse --short 26e1d228c71e69f0cb63fa73db8cc1ae3c6d8e87
26e1d22
</code></pre></td></tr></table></div></div><h2 id=git-tag>git tag</h2><p>Git的tag用于给某个分支的某个提交打上标签，可以理解为一个别名，类似于浏览器收藏某个网页作为书签。</p><p>通常tag用于标记某个阶段性的某次提交作为重要节点，比如release了某个版本等。因为commit-id本身很长，不利于记忆，将这个commit打上tag，如<code>v1.0.0</code>等，以后就可以直接搜索这个tag来快速切换到对应的代码。</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt>1
</span><span class=lnt>2
</span><span class=lnt>3
</span><span class=lnt>4
</span><span class=lnt>5
</span><span class=lnt>6
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 列出所有标签
git tag

// 使用-l或--list查询指定的标签
// 查询条件区分大小写，可以使用通配符，如*
git tag -l &#34;v1.0.*&#34;
</code></pre></td></tr></table></div></div><p>创建标签如下：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// 创建标签：分为轻量标签（lightweight）和附注标签（annotated）
// 轻量标签只是对某个commit的引用
// 附注标签是存在在Git数据库中的一个完整对象，包括了打标签者、打标签时间、标签的附注信息等额外的信息。
// 可以通过git show &lt;标签名&gt;来看到这两种标签的信息

// 创建附注标签
git tag -a &lt;标签名&gt; -m &#34;&lt;备注信息&gt;&#34;
// git tag -a v1.4 -m &#34;my version 1.4&#34;

// 创建轻量标签，不需要指定任何参数
git tag &lt;标签名&gt;
// git tag v1.4

// 上面都是对当前分支最新的commit创建标签，也可以对过去的某个commit打tag
git tag -a &lt;标签名&gt; &lt;commit-id&gt;
// git tag -a v1.4 26e1d22
</code></pre></td></tr></table></div></div><p>其他操作如下：</p><div class=highlight><div class=chroma><table class=lntable><tr><td class=lntd><pre class=chroma><code><span class=lnt> 1
</span><span class=lnt> 2
</span><span class=lnt> 3
</span><span class=lnt> 4
</span><span class=lnt> 5
</span><span class=lnt> 6
</span><span class=lnt> 7
</span><span class=lnt> 8
</span><span class=lnt> 9
</span><span class=lnt>10
</span><span class=lnt>11
</span><span class=lnt>12
</span><span class=lnt>13
</span><span class=lnt>14
</span><span class=lnt>15
</span><span class=lnt>16
</span><span class=lnt>17
</span><span class=lnt>18
</span><span class=lnt>19
</span><span class=lnt>20
</span><span class=lnt>21
</span><span class=lnt>22
</span><span class=lnt>23
</span><span class=lnt>24
</span><span class=lnt>25
</span><span class=lnt>26
</span><span class=lnt>27
</span><span class=lnt>28
</span><span class=lnt>29
</span></code></pre></td><td class=lntd><pre class=chroma><code class=language-fallback data-lang=fallback>// git push默认不会把tag推送到远程库（但是pull默认会获取tag），需要显示推送标签，操作和推送分支一样
// 推送指定的标签到远程库
git push &lt;远程库别名&gt; &lt;本地标签名&gt;
// git push origin v1.4

// 使用--tags参数来一次性推送所有不在远程库的标签
git push &lt;远程库别名&gt; --tags
// git push origin --tags


// 删除标签需要-d参数
git tag -d &lt;标签名&gt;
// git tag -d v1.4

// 删除远程库标签有两种方式
// 方式一：将空标签推送到指定的远程库标签
git push &lt;远程库别名&gt; :refs/tags/&lt;标签名&gt;
// git push origin :refs/tags/v1.4

// 方式二：使用--delete，需要Git版本大于v1.7.0才能使用
git push &lt;远程库别名&gt; --delete &lt;标签名&gt;
// git push origin --delete v1.4


// 检出标签
// tag是快照读，直接检出到指定的tag会使仓库处于detached HEAD状态，此时不能更改代码
// 如果需要回到某个tag并修改代码，需要创建对应的分支。当在该分支上提交后，可能需要一个新的tag来标注最新的提交
git checkout -b &lt;新建分支名&gt; &lt;标签名&gt;
// git checkout -b version1.4 v1.4
</code></pre></td></tr></table></div></div><h2 id=参考链接>参考链接</h2><ul><li><a href=http://www.ruanyifeng.com/blog/2014/06/git_remote.html target=_blank rel="noopener noreffer">Git远程操作详解</a></li><li><a href=https://www.jianshu.com/p/2e1d551b8261 target=_blank rel="noopener noreffer">git的add、commit、push的详细介绍</a></li><li><a href=https://blog.csdn.net/weixin_40295575/article/details/91816937 target=_blank rel="noopener noreffer">git的hard、soft、mixed参数比较</a></li><li><a href=https://www.cnblogs.com/mini-monkey/p/12032051.html target=_blank rel="noopener noreffer">git_04_回退到上个版本</a></li><li><a href=https://blog.csdn.net/hl_java/article/details/91424506 target=_blank rel="noopener noreffer">Git基础操作：将git commit id转成short commit id</a></li><li><a href=https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE target=_blank rel="noopener noreffer">2.6 Git 基础 - 打标签</a></li></ul><div class=post-reward><input type=checkbox name=reward id=reward hidden>
<label class=reward-button for=reward>赞赏支持</label><div class=qr-code><label class=qr-code-image for=reward><img class=image src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/common/wechat.png>
<span>微信打赏</span></label>
<label class=qr-code-image for=reward><img class=image src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/common/alipay.png>
<span>支付宝打赏</span></label></div></div></div><div class=post-footer id=post-footer><div class=post-info><div class=post-info-line><div class=post-info-mod><span>更新于 2022-01-04</span></div><div class=post-info-license><span><a rel="license external nofollow noopener noreffer" href=https://creativecommons.org/licenses/by-nc/4.0/ target=_blank>CC BY-NC 4.0</a></span></div></div><div class=post-info-line><div class=post-info-md><span><a class=link-to-markdown href=/posts/git-commands/index.md target=_blank>阅读原始文档</a></span></div><div class=post-info-share><span><a href=javascript:void(0); title="分享到 Twitter" data-sharer=twitter data-url=https://lewky.cn/posts/git-commands/ data-title=Git常用命令 data-hashtags=Git,版本控制工具><i class="fab fa-twitter fa-fw"></i></a><a href=javascript:void(0); title="分享到 Facebook" data-sharer=facebook data-url=https://lewky.cn/posts/git-commands/ data-hashtag=Git><i class="fab fa-facebook-square fa-fw"></i></a><a href=javascript:void(0); title="分享到 Hacker News" data-sharer=hackernews data-url=https://lewky.cn/posts/git-commands/ data-title=Git常用命令><i class="fab fa-hacker-news fa-fw"></i></a><a href=javascript:void(0); title="分享到 Line" data-sharer=line data-url=https://lewky.cn/posts/git-commands/ data-title=Git常用命令><i data-svg-src=https://cdn.jsdelivr.net/npm/simple-icons@2.14.0/icons/line.svg></i></a><a href=javascript:void(0); title="分享到 微博" data-sharer=weibo data-url=https://lewky.cn/posts/git-commands/ data-title=Git常用命令><i class="fab fa-weibo fa-fw"></i></a></span></div></div></div><div class=post-info-more><section class=post-tags><i class="fas fa-tags fa-fw"></i>&nbsp;<a href=/tags/git/>Git</a>,&nbsp;<a href=/tags/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E5%B7%A5%E5%85%B7/>版本控制工具</a></section><section><span><a href=javascript:void(0); onclick=window.history.back();>返回</a></span>&nbsp;|&nbsp;<span><a href=/>主页</a></span></section></div><div class=post-nav><a href=/posts/network/ class=prev rel=prev title=计算机网络基础><i class="fas fa-angle-left fa-fw"></i>计算机网络基础</a>
<a href=/posts/postgresql-dml/ class=next rel=next title="PostgreSQL - DML操作汇总">PostgreSQL - DML操作汇总<i class="fas fa-angle-right fa-fw"></i></a></div></div><div id=comments><div id=waline></div><script src=https://cdn.jsdelivr.net/npm/@waline/client/dist/Waline.min.js></script><script>new Waline({el:'#waline',meta:["nick","mail","link"],requiredMeta:["nick","mail"],login:"force",placeholder:"为防恶意灌水攻击，评论前需注册并登录，望见谅~",serverURL:"https://comment.lewky.cn/",avatarCDN:"https://sdn.geekzu.org/avatar/",pageSize:20,avatar:"retro",lang:"zh-CN",visitor:true,highlight:true,uploadImage:false,emoji:['https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/嘉然今天吃什么','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/大航海嘉然','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/向晚大魔王','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/贝拉kira','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/珈乐Carol','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/乃琳Queen','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/EveOneCat','https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/weibo','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/滑稽','https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/emoji/default']});</script></div></article></div></main><footer class=footer><div class=footer-container><div class=footer-line><span id=run-time></span>|
<a href=http://rssblog.vercel.app/ target=_blank rel=noopener title=RSSBlog><i class="fas fa-fw fa-inbox"></i>&nbsp;RSSBlog</a></div><div class=footer-line><i class="far fa-copyright fa-fw"></i><span itemprop=copyrightYear>2018 - 2022</span><span class=author itemprop=copyrightHolder>&nbsp;<a href=/ target=_blank>雨临Lewis</a></span>&nbsp;|&nbsp;<span class=license><a rel="license external nofollow noopener noreffer" href=https://creativecommons.org/licenses/by-nc/4.0/ target=_blank>CC BY-NC 4.0</a></span><span class=icp-splitter>&nbsp;|&nbsp;</span><br class=icp-br><span class=icp><a target=_blank href=http://www.beian.miit.gov.cn/ style=font-weight:700>粤ICP备19103822</a></span></div></div></footer></div><div id=fixed-buttons><a href=# id=back-to-top class=fixed-button title=回到顶部><i class="fas fa-arrow-up fa-fw"></i></a><a href=# id=view-comments class=fixed-button title=查看评论><i class="fas fa-comment fa-fw"></i></a></div><div class=sidebar_wo><div id=leimu><img src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/b2t/leimuA.png alt=雷姆 onmouseover="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/leimuB.png'" onmouseout="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/leimuA.png'" title=回到顶部></div><div class=sidebar_wo id=lamu><img src=https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master/images/b2t/lamuA.png alt=雷姆 onmouseover="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/lamuB.png'" onmouseout="this.src='https:\/\/cdn.jsdelivr.net\/gh\/lewky\/lewky.github.io@master/images/b2t/lamuA.png'" title=回到底部></div></div><link rel=stylesheet href=https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/css/lightgallery.min.css><script type=text/javascript src=https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/js/lightgallery.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lg-thumbnail.js@1.2.0/dist/lg-thumbnail.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/lg-zoom.js@1.2.0/dist/lg-zoom.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js></script><script type=text/javascript>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":30},"comment":{},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true}};</script><script type=text/javascript src=/js/theme.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/jquery@2.1.3/dist/jquery.min.js></script><script type=text/javascript src=https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js></script><script>var $cdnPrefix="https://cdn.jsdelivr.net/gh/lewky/lewky.github.io@master";</script><script type=text/javascript src=/js/custom.js></script><script>var _hmt=_hmt||[];(function(){var hm=document.createElement("script");hm.src="https://hm.baidu.com/hm.js?6f278ed0fdb01edb3b1e7398379e5432";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm,s);})();</script></body></html>